package org.xiezc.listerner;

import org.apache.log4j.Logger;
import org.springframework.web.context.ContextLoaderListener;

import javax.servlet.ServletContextEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;


/**
 * 初始化监听器类，此监听器继承了spring的监听器
 *
 * @author xiezc
 */
public class XContextLoaderListener extends ContextLoaderListener {

    private static Logger log = Logger.getLogger(XContextLoaderListener.class);

    @Override
    public void contextInitialized(ServletContextEvent event) {
        super.contextInitialized(event);
        log.info("Spring初始化完毕。开始加载微信的配置");

        InputStreamReader isr = null;
        try {
            Properties properties = new Properties();
            isr = new InputStreamReader(
                    new FileInputStream("com/xiezc/config/config.properties"));
            properties.load(isr);

            //TODO 启动加载的一些配置信息 和 一些初始化信息


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (isr != null) {
                try {
                    isr.close();
                } catch (IOException e) {
                    log.debug("isr.close()关闭资源报错,资源可能已经关闭");
                }

            }
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        super.contextDestroyed(event);
        log.info("spring 的监听器关闭 ");
    }

}
